
const { contextBridge, ipcRenderer } = require('electron')
const path = require('path')
/**
 * @添加contextBridge API
 * @getVersions 获取版本
 * @sendMessageToMain 发送消息到主进程
 * @onMessageFromMain 监听主进程返回消息
 * @getAppVersion 获取应用版本
 * @openFile 打开文件
 * @onUdpMessage 监听UDP消息
 * @removeUdpListener 移除UDP监听
 * @reloadPage 刷新页面
 * @toggleDarkMode 切换主题
 * @systemDarkMode 切换到系统主题
 * @starDrag 拖拽文件到桌面
 */
contextBridge.exposeInMainWorld('electron', {
  getVersions: () => ({
    node: process.versions.node,
    chrome: process.versions.chrome,
    electron: process.versions.electron
  }),
  getAppVersion: () => ipcRenderer.invoke('get-app-version'),
  openFile: (filePath) => ipcRenderer.invoke('open-file', filePath),
  sendMessageToMain: (message) => ipcRenderer.send('message-to-main', message),
  onMessageFromMain: (callback) => {
    ipcRenderer.on('message-from-main', (event, message) => callback(message))
  },
  onUdpMessage: (callback) => {
    ipcRenderer.on('udp-message', (_event, data) => callback(data))
  },
  removeUdpListener: () => {
    ipcRenderer.removeAllListeners('udp-message')
  },
  openFileDialog: () => ipcRenderer.invoke('dialog:openFile'),
  reloadPage: (callback) => {
    ipcRenderer.on('reload-page', () => callback())
  },
  toggleDarkMode:()=>ipcRenderer.invoke('dark-mode:toggle'),
  systemDarkMode:()=>ipcRenderer.invoke('dark-mode:system'),
  onDarkModeChange:(callback)=>{
    ipcRenderer.on('dark-mode:change',(event,data)=>callback(data))
  },
  starDrag: (fileName) => {
    ipcRenderer.send('ondragstart', fileName)
  }
})
